package cn.tedu.boot32.controller;

import cn.tedu.boot32.entity.User;
import cn.tedu.boot32.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class userController {
    @Autowired(required = false)
    UserMapper mapper;

    @RequestMapping("/reg")
    public String reg(User user){
        System.out.println("user = " + user);
        //通过用户输入的用户名查询是否存在数据  u代表查询回来的用户信息
        User u = mapper.selectByUsername(user.getUsername());
        if(u!=null){
            return "用户名存在,<a href='/reg.html'>点击重新输入</a>";
        }
        //用户名不存在,可以注册
        mapper.insert(user);

        return "注册成功,<a href='/index.html'>点击返回首页</a>";
    }

    @RequestMapping("/login")
    public String login(User user){
        User u = mapper.selectByUsername(user.getUsername());
        if(u!=null){
            if(u.getPassword().equals(user.getPassword())){
                return "登录成功,<a href='/index.html'>点击返回首页</a>";

            }
            return "密码错误,<a href='/login.html'>点击重新输入密码</a>";
        }
        return "用户名不存在,<a href='/login.html'>点击重新登录</a>";

    }

}
